package com.hello.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hello.common.log.annotation.Log;
import com.hello.common.log.enums.BusinessType;
import com.hello.common.security.annotation.RequiresPermissions;
import com.hello.system.domain.RentalOrders;
import com.hello.system.domain.dto.RentalOrdersWithDetailsDto;
import com.hello.system.service.IRentalOrdersService;
import com.hello.common.security.utils.SecurityUtils;
import com.hello.common.core.web.controller.BaseController;
import com.hello.common.core.web.domain.AjaxResult;
import com.hello.common.core.utils.poi.ExcelUtil;
import com.hello.common.core.web.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 租赁订单Controller
 * 
 * @author hello
 * @date 2025-07-20
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/orders")
public class RentalOrdersController extends BaseController
{
    private final IRentalOrdersService rentalOrdersService;

    /**
     * 查询租赁订单列表
     */
    // @RequiresPermissions("system:orders:list")
    @GetMapping("/list")
    public TableDataInfo list(RentalOrders rentalOrders)
    {
        log.info("======== 查询租赁订单列表请求 ========");
        log.info("请求参数: {}", rentalOrders);
        
        startPage();
        List<RentalOrders> list = rentalOrdersService.selectRentalOrdersList(rentalOrders);
        TableDataInfo result = getDataTable(list);
        
        log.info("查询结果: 总记录数={}, 当前页数据量={}", result.getTotal(), list.size());
        log.info("======== 查询租赁订单列表完成 ========");
        
        return result;
    }

    /**
     * 查询当前用户的租赁订单列表（根据token获取用户信息）
     * 包含设备名称和地址详细信息
     */
    @GetMapping("/my")
    public TableDataInfo getMyOrders(RentalOrders rentalOrders)
    {
        log.info("======== 查询当前用户订单列表请求 ========");
        log.info("请求参数: {}", rentalOrders);
        
        try {
            // 从token中获取当前用户ID
            Long userId = SecurityUtils.getUserId();
            log.info("当前用户ID: {}", userId);
            
            // 设置查询条件为当前用户
            rentalOrders.setUserId(userId);
            
            startPage();
            List<RentalOrdersWithDetailsDto> list = rentalOrdersService.selectMyRentalOrdersWithDetails(rentalOrders);
            TableDataInfo result = getDataTable(list);
            
            log.info("查询结果: 总记录数={}, 当前页数据量={}", result.getTotal(), list.size());
            log.info("======== 查询当前用户订单列表完成 ========");
            
            return result;
        } catch (Exception e) {
            log.error("查询当前用户订单列表失败: {}", e.getMessage(), e);
            return getDataTable(java.util.Collections.emptyList());
        }
    }

    /**
     * 导出租赁订单列表
     */
    // @RequiresPermissions("system:orders:export")
    @Log(title = "租赁订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RentalOrders rentalOrders)
    {
        log.info("======== 导出租赁订单数据请求 ========");
        log.info("导出条件: {}", rentalOrders);
        
        List<RentalOrders> list = rentalOrdersService.selectRentalOrdersList(rentalOrders);
        log.info("导出数据量: {} 条记录", list.size());
        
        ExcelUtil<RentalOrders> util = new ExcelUtil<RentalOrders>(RentalOrders.class);
        util.exportExcel(response, list, "租赁订单数据");
        
        log.info("======== 租赁订单数据导出完成 ========");
    }

    /**
     * 获取租赁订单详细信息
     */
    // @RequiresPermissions("system:orders:query")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        log.info("======== 获取租赁订单详情请求 ========");
        log.info("订单ID: {}", orderId);
        
        RentalOrders order = rentalOrdersService.selectRentalOrdersByOrderId(orderId);
        log.info("查询结果: {}", order != null ? "找到订单信息" : "未找到对应订单");
        
        log.info("======== 获取租赁订单详情完成 ========");
        return success(order);
    }

    /**
     * 新增租赁订单
     */
    // @RequiresPermissions("system:orders:add")
    @Log(title = "租赁订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RentalOrders rentalOrders)
    {
        log.info("======== 新增租赁订单请求 ========");
        log.info("新增数据: {}", rentalOrders);
        
        int result = rentalOrdersService.insertRentalOrders(rentalOrders);
        log.info("新增结果: {}", result > 0 ? "成功" : "失败");
        
        log.info("======== 新增租赁订单完成 ========");
        return toAjax(result);
    }

    /**
     * 修改租赁订单
     */
    // @RequiresPermissions("system:orders:edit")
    @Log(title = "租赁订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RentalOrders rentalOrders)
    {
        log.info("======== 修改租赁订单请求 ========");
        log.info("修改数据: {}", rentalOrders);
        
        int result = rentalOrdersService.updateRentalOrders(rentalOrders);
        log.info("修改结果: {}", result > 0 ? "成功" : "失败");
        
        log.info("======== 修改租赁订单完成 ========");
        return toAjax(result);
    }

    /**
     * 删除租赁订单
     */
    // @RequiresPermissions("system:orders:remove")
    @Log(title = "租赁订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        log.info("======== 删除租赁订单请求 ========");
        log.info("删除订单ID列表: {}", java.util.Arrays.toString(orderIds));
        log.info("删除数量: {} 个订单记录", orderIds.length);
        
        int result = rentalOrdersService.deleteRentalOrdersByOrderIds(orderIds);
        log.info("删除结果: {}", result > 0 ? "成功" : "失败");
        
        log.info("======== 删除租赁订单完成 ========");
        return toAjax(result);
    }
}
